package com.yx.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yx.common.vo.Result;
import com.yx.sys.entity.Post;
import com.yx.sys.entity.Reply;
import com.yx.sys.service.IPostService;
import com.yx.sys.service.IReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 回复 前端控制器
 * </p>
 *
 * @author yx
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/reply")
public class ReplyController {
    @Autowired
    private IReplyService iReplyService;

    @Autowired
    private IPostService postService;

    @GetMapping("/all")
    public Result<List<?>> selectReplyMes(@RequestParam(value ="status",required = false) Integer status){
        System.out.println(status);
        List<?> allReply = iReplyService.getReplyMes(status);
        return Result.success(allReply,"获取全部评论成功");
    }
    @GetMapping("/list")
    public Result<List<Reply>> selectReplyBypId(Integer pId){
        List<Reply> list =  iReplyService.getReplyBypId(pId);
        return Result.success(list,"根据pId查询评论成功");
    }

    @GetMapping("/parent")
    public Result<Map<String,Object>> selectParentByrId(Integer rId){
        Map<String,Object> parentByrId = iReplyService.getParentByrId(rId);
//        System.out.println(rId);
//        System.out.println("评论："+parentByrId);
        return Result.success(parentByrId,"获取父级评论");
    }

//    根据用户uId查询回复情况
    @GetMapping("/{uId}")
    public Result<List<?>> selectReplyByuId(@PathVariable("uId") Integer uId){
//        用户头像，昵称，评论内容，时间，评论来源（1、文章  2、父级评论）
//        先多表联查，查询头像和评论内容
//        再查询父级或post内容
        List<?> list = iReplyService.getReplyByuId(uId);
        System.out.println(list);
        return Result.success(list);
    }
//    发表评论
    @PostMapping("/addComment")
    public Result<?> addComment(@RequestBody Reply reply){
        System.out.println(reply);
//        查询之前是否插入过该评论
        QueryWrapper<Reply> wrapper = new QueryWrapper<>();
        wrapper.eq("rContent",reply.getrContent()).eq("pId",reply.getpId()).eq("from_uId",reply.getFromuId());
        Reply reply1 = iReplyService.getOne(wrapper);
        System.out.println(reply1);
        if(reply1==null){
            boolean save = iReplyService.save(reply);
            if(save){
                QueryWrapper<Post> postWrapper = new QueryWrapper<>();
                postWrapper.eq("pId",reply.getpId());
                Integer replyNums = postService.getOne(postWrapper).getReplyNums();
                System.out.println("replyNums:"+replyNums);
                LambdaUpdateWrapper<Post> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(Post::getpId,reply.getpId()).set(Post::getReplyNums,replyNums+1);
                postService.update(updateWrapper);
                System.out.println("-------------------------");
            }
            return Result.success(20000,"添加评论成功");
        }else{
            return Result.success(20001,"该评论已经存在");
        }
    }

//    根据rId修改评论状态
    @PutMapping("/delReply")
    public Result<?> delComment(@RequestParam("rId") Integer rId,
                                @RequestParam("num") Integer num){
        System.out.println("rId:"+rId);
        LambdaUpdateWrapper<Reply> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Reply::getrId,rId);
        Reply reply = iReplyService.getOne(wrapper);
        System.out.println(reply);
        boolean update = true;
        if(reply!=null){
            wrapper.set(Reply::getIsDelete,num);
            update = iReplyService.update(wrapper);
        }
        return Result.success(update,"删除违规评论成功");
    }
}
